package com.patrol.logisticsmanage.ui.todo;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.patrol.logisticsmanage.R;
import com.patrol.logisticsmanage.net.BaseObserver;
import com.patrol.logisticsmanage.net.BaseReponse;
import com.patrol.logisticsmanage.net.BaseRequest;
import com.patrol.logisticsmanage.ui.appear.model.ServiceOrder;
import com.patrol.logisticsmanage.ui.base.BaseActivity;
import com.patrol.logisticsmanage.ui.canteen.utils.GsonUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

@Route(path = "/app/MyAppearActivity")
public class MyAppearActivity extends BaseActivity implements ToDoAdapter.OnItemClickListener {

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.img_submit)
    ImageView imgSubmit;
    @BindView(R.id.rcy_todo_list)
    RecyclerView rcyTodoList;

    ToDoAdapter toDoAdapter;
    List<ServiceOrder> serviceOrders;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_appear);
        ButterKnife.bind(this);

        tvTitle.setText("我的报修");

        serviceOrders = new ArrayList<>();
        rcyTodoList.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        toDoAdapter = new ToDoAdapter(this,serviceOrders,this);
        rcyTodoList.setAdapter(toDoAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void getData(){
        BaseRequest.getInstance().getApiServise().myapplylist()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Map>(mContext) {
                    @Override
                    public void onSuccess(BaseReponse<Map> baseReponse) {
                        List result = (List) baseReponse.getResults().get("records");
                        List<ServiceOrder> list = GsonUtil.mapListToBeanList(result, ServiceOrder.class);
                        serviceOrders.clear();
                        serviceOrders.addAll(list);
                        toDoAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onCodeError(BaseReponse tBaseReponse) { }
                    @Override
                    public void onFailure(Throwable e, boolean netWork) throws Exception { }
                });
    }


    @OnClick(R.id.img_submit)
    public void onViewClicked() {
        ARouter.getInstance().build("/app/AppearActivity").navigation();
    }

    @Override
    public void onItemClick(View v, int position) {
        ARouter.getInstance().build("/app/MyAppearDetailActivity")
                .withString("order", GsonUtil.objectToGson(serviceOrders.get(position))).navigation();
    }
}
